﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace simWaifu
{
    public partial class FormBasicSettings : Form
    {
        FormMain formMain = null;

        public FormBasicSettings()
        {
            InitializeComponent();
        }

        public FormBasicSettings(FormMain fM)
        {
            InitializeComponent();
            formMain = fM;
        }

        private void textBoxWaifuGender_TextChanged(object sender, EventArgs e)
        {
            Waifu.gender = textBoxWaifuGender.Text;
            
            // mh, maybe add a change Gender method to Waifu which updates the properties?
            Properties.Settings.Default.WaifuGender = Waifu.gender;
            formMain.UpdateTitlebar();
        }

        private void textBoxWaifuName_TextChanged(object sender, EventArgs e)
        {
            Waifu.waifuName = textBoxWaifuName.Text;
            Properties.Settings.Default.WaifuName = Waifu.waifuName;
            formMain.UpdateTitlebar();
        }

        private void textBoxWaifuDataFolder_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void FormBasicSettings_Load(object sender, EventArgs e)
        {
            // disable FormMain´s TopMost (if set) to avoid being hidden by a too big form
            formMain.TopMostDisable();

            // not binding these through assembly settings because of future port to C++/Qt

            textBoxWaifuGender.Text = Properties.Settings.Default.WaifuGender;
            textBoxWaifuName.Text = Properties.Settings.Default.WaifuName;
            checkBoxShowMood.Checked = Properties.Settings.Default.ShowMoodName;
            buttonMessageDemo.Font = Properties.Settings.Default.FontMessage;
            buttonMessageDemo.ForeColor = Properties.Settings.Default.FontMessageColor;
            textBoxWaifuDataFolder.Text = Properties.Settings.Default.WaifuDataFolder;
            textBoxWaifuDataFolder.Text = Properties.Settings.Default.WaifuDataFolder;

            // Typer/Fader stuff
            checkBoxActivateTyper.Checked = Properties.Settings.Default.ActivateTyper;
            if (Properties.Settings.Default.TyperPosition.Equals("Center"))
            {
                radioButtonTyperCenter.Checked = true;
                radioButtonTyperLeft.Checked = false;
            }
            else
            {
                radioButtonTyperCenter.Checked = false;
                radioButtonTyperLeft.Checked = true;
            }
            numericFadeDelay.Value = Properties.Settings.Default.FaderDelay;
            numericTypeDelay.Value = Properties.Settings.Default.TyperDelay;

            // cursor
            checkBoxActivateCursor.Checked = Properties.Settings.Default.ActivateCursor;
            textBoxCursorType.Text = Properties.Settings.Default.CursorType;

            // change inside mood
            checkBoxChangeInsideMood.Checked = Properties.Settings.Default.AutoChangeInsideMood;
            numericChangeInsideMoodMin.Value = Properties.Settings.Default.AutoChangeInsideMoodMinTime;
            numericChangeInsideMoodMax.Value = Properties.Settings.Default.AutoChangeInsideMoodMaxTime;

            // change mood itself
            checkBoxChangeMoodAutoActivated.Checked = Properties.Settings.Default.AutoChangeOfMood;
            numericChangeMoodAfterMin.Value = Properties.Settings.Default.AutoChangeOfMoodMinTime;
            numericChangeMoodAfterMax.Value = Properties.Settings.Default.AutoChangeOfMoodMaxTime;

            // welcome mode
            checkBoxWelcomeModeActive.Checked = Properties.Settings.Default.WelcomeModeActive;
            numericWelcomeDuration.Value = Properties.Settings.Default.WelcomeModeDuration;

            // sleep cycle stuff
            if(Properties.Settings.Default.SleepCycleActivated)
            {
                radioSleepNever.Checked = false;
            }

            dateTimePickerWakeUp.Format = DateTimePickerFormat.Time;
            dateTimePickerGoToBed.Format = DateTimePickerFormat.Time;

            dateTimePickerWakeUp.Value = Properties.Settings.Default.SleepCycleFixedWakeUpTime;
            dateTimePickerGoToBed.Value = Properties.Settings.Default.SleepCycleFixedGoToBedTime;

            labelTimeTest.Text = "Next wake up:" + dateTimePickerWakeUp.Value.ToString();
            labelTimeTest2.Text = "Next sleep:" + dateTimePickerGoToBed.Value.ToString();
                        
            //SleepCycleEnabled bool
            //SleepCycleFixedActive bool
            //SleepCycleFixedWakeUpTime
            //SleepCycleFixedGoToBedTime

            //SleepCycleRelativeActive bool
            //SleepCycleRelativeAwakeMin milliseconds
            //SleepCycleRelativeAwakeMax milliseconds
            //SleepCycleRelativeSleepMin milliseconds
            //SleepCycleRelativeSleepMax milliseconds

            //SpecialDateEvent1Active bool
            //SpecialDateEvent1Date Date
            //SpecialDateEvent1Mood string

            //SpecialDateEvent2Active bool
            //SpecialDateEvent2Date Date
            //SpecialDateEvent2Mood string

            //SpecialDateEvent3Active bool
            //SpecialDateEvent3Date Date
            //SpecialDateEvent3Mood string

            //SpecialDateEvent4Active bool
            //SpecialDateEvent4Date Date
            //SpecialDateEvent4Mood string
        }

        private void buttonSetWaifuDataFolder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.WaifuDataFolder = folderBrowserDialog1.SelectedPath;
                Waifu.Initialize(); // rescan data
                formMain.UpdateTitlebar();
            }
        }

        private void checkBoxShowMood_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.ShowMoodName = checkBoxShowMood.Checked;
            formMain.UpdateTitlebar();
        }

        private void buttonSetMessageFont_Click(object sender, EventArgs e)
        {
            fontDialog1.Font = Properties.Settings.Default.FontMessage;
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.FontMessage = fontDialog1.Font;
                formMain.UpdateMessageFont();
                buttonMessageDemo.Font = Properties.Settings.Default.FontMessage;
                buttonMessageDemo.ForeColor = Properties.Settings.Default.FontMessageColor;
            }
        }

        private void buttonSetMessageColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.FontMessageColor = colorDialog1.Color;
                formMain.UpdateMessageFont();
                buttonMessageDemo.Font = Properties.Settings.Default.FontMessage;
                buttonMessageDemo.ForeColor = Properties.Settings.Default.FontMessageColor;
            }
        }

        private void buttonResetMessageFont_Click(object sender, EventArgs e)
        {
            // reset back to embedded font
        }

        private void FormBasicSettings_Closing(object sender, FormClosingEventArgs e)
        {
            formMain.TopMostEnable();
        }

        private void checkBoxActivateTyper_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.ActivateTyper = checkBoxActivateTyper.Checked;
        }

        private void radioButtonTyperCenter_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.TyperPosition = "Center";
        }

        private void radioButtonTyperLeft_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.TyperPosition = "Left";
        }

        private void checkBoxActivateCursor_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.ActivateCursor = checkBoxActivateCursor.Checked;
        }

        private void textBoxCursorType_TextChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.CursorType = textBoxCursorType.Text;
        }

        private void numericTypeDelay_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.TyperDelay = (int)numericTypeDelay.Value;
        }

        private void numericFadeDelay_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.FaderDelay = (int)numericFadeDelay.Value;
        }

        private void checkBoxChangeInsideMood_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.AutoChangeInsideMood = checkBoxChangeInsideMood.Checked;
            formMain.InitializeTimers();
        }

        private void checkBoxChangeMoodAutoActivated_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.AutoChangeOfMood = checkBoxChangeMoodAutoActivated.Checked;
            formMain.InitializeTimers();
        }

        private void numericChangeInsideMoodMin_ValueChanged(object sender, EventArgs e)
        {
            int value = (int)numericChangeInsideMoodMin.Value;
            Properties.Settings.Default.AutoChangeInsideMoodMinTime = value;
            if (value > Properties.Settings.Default.AutoChangeInsideMoodMaxTime)
            {
                Properties.Settings.Default.AutoChangeInsideMoodMaxTime = value;
                numericChangeInsideMoodMax.Value = numericChangeInsideMoodMin.Value;
            }           
        }

        private void numericChangeInsideMoodMax_ValueChanged(object sender, EventArgs e)
        {
            int value = (int)numericChangeInsideMoodMax.Value;
            Properties.Settings.Default.AutoChangeInsideMoodMaxTime = value;
            if (value < Properties.Settings.Default.AutoChangeInsideMoodMinTime)
            {
                Properties.Settings.Default.AutoChangeInsideMoodMinTime = value;
                numericChangeInsideMoodMin.Value = value;
            }           
        }

        private void numericChangeMoodAfterMin_ValueChanged(object sender, EventArgs e)
        {
            int value = (int)numericChangeMoodAfterMin.Value;
            Properties.Settings.Default.AutoChangeOfMoodMinTime = value;
            if (value > Properties.Settings.Default.AutoChangeOfMoodMaxTime)
            {
                Properties.Settings.Default.AutoChangeOfMoodMaxTime = value;
                numericChangeMoodAfterMax.Value = value;
            }           
        }

        private void numericChangeMoodAfterMax_ValueChanged(object sender, EventArgs e)
        {
            int value = (int)numericChangeMoodAfterMax.Value;
            Properties.Settings.Default.AutoChangeOfMoodMaxTime = value;
            if (value < Properties.Settings.Default.AutoChangeOfMoodMinTime)
            {
                Properties.Settings.Default.AutoChangeOfMoodMinTime = value;
                numericChangeMoodAfterMin.Value = value;
            }
        }

        private void checkBoxWelcomeModeActive_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.WelcomeModeActive = checkBoxWelcomeModeActive.Checked;
        }

        private void numericWelcomeDuration_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.WelcomeModeDuration = (int)numericWelcomeDuration.Value;
        }

        private void radioSleepNever_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.SleepCycleActivated = !radioSleepNever.Checked;
            // disable the other two if "never sleeps" is active
            if (!Properties.Settings.Default.SleepCycleActivated)
            {
                Properties.Settings.Default.SleepCycleFixedActive = false;
                Properties.Settings.Default.SleepCycleRelativeActive = false;
            }
        }

        private void radioSleepFixed_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.SleepCycleFixedActive = radioSleepFixed.Checked;
            // if activated, handle the other two
            if (Properties.Settings.Default.SleepCycleFixedActive)
            {
                Properties.Settings.Default.SleepCycleActivated = true;
                // todo: allow to take short naps during daytime
                Properties.Settings.Default.SleepCycleRelativeActive = false;
            }
        }

        private void radioSleepRelative_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.SleepCycleRelativeActive = radioSleepRelative.Checked;
            // if activated, handle the other two
            if (Properties.Settings.Default.SleepCycleRelativeActive)
            {
                Properties.Settings.Default.SleepCycleActivated = true;
                // todo: allow to take short naps during daytime
                Properties.Settings.Default.SleepCycleFixedActive = false;
            }
        }
         
        private void dateTimePickerWakeUp_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.SleepCycleFixedWakeUpTime = dateTimePickerWakeUp.Value;
        }

        private void dateTimePickerGoToBed_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.SleepCycleFixedGoToBedTime = dateTimePickerGoToBed.Value;
        }

        private void numericSleepRelativeAwakeMinHours_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numericSleepRelativeAwakeMinMinutes_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numericSleepRelativeAwakeMaxHours_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numericSleepRelativeAwakeMaxMinutes_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numericSleepRelativeAsleepMinHours_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numericSleepRelativeAsleepMinMinutes_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numericSleepRelativeAsleepMaxHours_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numericSleepRelativeAsleepMaxMinutes_ValueChanged(object sender, EventArgs e)
        {

        }
    }
}

